function ajax(url = '', method = 'GET', data = {}) {
    return new Promise((resolve, reject) => {
        let options = { method }
        if (method === 'GET') {
            url += '?' + Object.entries(data).map(arr => arr[0] + '=' + arr[1]).join('&')
        } else if (method === 'POST') {
            options.body = JSON.stringify(data)
            options.headers = { 'Content-Type': 'application/json' }
        }
        fetch(url, options).then(res => res.json())
            .then(data => resolve(data))
            .catch(e => reject(e))
    })
}

// 使用范例
ajax('http://api2,jirengu.com/getWeather.php', 'GET', { city: '杭州' })
    .then(data => console.log(data))
    .catch(e => console.log(e))

ajax('https://note-server.hunger-valley.com/auth/login', 'POST', { username: 'jirengu', password: '123456' })
    .then(data => console.log(data))
    .catch(e => console.log(e))